package com.woniuxy.lrz.carlproject.common.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class BeanCopyUtil {

    public static <T>T copyOne(Object source,Class<T> clazz){

        T t = null;
        try {
            t = clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        BeanUtils.copyProperties(source,t);

        return t;

    }

    public static <T> List<T> copyList(List list, Class<T> clazz){

        ArrayList<T> newList = new ArrayList<>();

        for (Object o : list) {

            T t = null;
            try {
                t = clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            BeanUtils.copyProperties(o,t);

            newList.add(t);
        }

        return newList;

    }

    public static <T> Page<T> copyPageInfo(Page page, Class<T> clazz){
        Page<T> tPage = new Page<>();
        tPage.setCurrent(page.getCurrent());
        tPage.setSize(page.getSize());
        tPage.setTotal(page.getTotal());
        tPage.setPages(page.getPages());
        tPage.setRecords(copyList(page.getRecords(),clazz));
        return tPage;
    }

}
